package org.streets.workflow.engine.extension;

import org.streets.workflow.engine.ISyncRouter;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.engine.event.INodeEventListener;
import org.streets.workflow.engine.event.NodeEvent;
import org.streets.workflow.engine.mem.SyncRouter;
import org.streets.workflow.engine.persistence.IWorkflowDAO;
import org.streets.workflow.engine.plugin.INodeNetExtension;

/**
 *
 */
public class SyncRouterExtension implements INodeNetExtension, INodeEventListener {

    public String getExtentionPointName() {
        return SyncRouter.Extension_Point_NodeInstanceEventListener;
    }

    /* (non-Javadoc)
     * @see org.fireflow.kenel.plugin.IKenelExtension#getExtentionTargetName()
     */
    public String getExtentionTargetName() {
        return SyncRouter.Extension_Target_Name;
    }

    /* (non-Javadoc)
     * @see org.fireflow.kenel.event.INodeInstanceEventListener#onNodeInstanceEventFired(org.fireflow.kenel.event.NodeInstanceEvent)
     */
    public void onNodeEventFired(WorkflowEngine engine, NodeEvent event) throws WorkflowException {
    	//同步器节点的监听器触发条件，是在离开这个节点的时候
        if (event.getEventType() == NodeEvent.NODEINSTANCE_LEAVING) {
            ISyncRouter syncInst = (ISyncRouter) event.getSource();
            IWorkflowDAO persistence = engine.getRuntimeContext().getPersistenceService();
            //删除同步器节点的token
            persistence.deleteTokens(event.getToken().getProcess().getId(), syncInst.getNodeId());
        }
    }
}
